<?php

namespace Biz\Entry\Service\Impl;

use Codeages\Biz\Framework\Service\BaseService;
use Biz\Entry\Service\EntryService;

class EntryServiceImpl extends BaseService implements EntryService
{

    public function countEntries($conditions){
        $conditions=$this->_prepareSearchConditions($conditions);
        return $this->getEntryDao()->count($conditions);
    }

    public function searchEntries($condition,$orderBy,$start,$limit){
        $condition=$this->_prepareSearchConditions($condition);
        $entries = $this->getEntryDao()->search($condition,$orderBy,$start,$limit);
        return $entries;
    }

    /*
     * 获取单条留言
     */
    public function getEntry($id)
    {
        return $this->getEntryDao()->get($id);
    }



    /*
     *创建留言提交保存到数据库，包含内容和标题和作者
     */
    public function createEntry($fields)
    {
        $fields['author']=1;//一个数组包含标题和内容，再加入一个键值对author
        return $this->getEntryDao()->create($fields);
    }



    /*
     *编辑留言提交保存到数据库，更新内容和标题
     */
    public function updateEntry($id,$fields){
        return $this->getEntryDao()->update($id,$fields);
    }

    /*
     * 删除一条留言
     */
    public function deleteEntry($id)
    {
        return $this->getEntryDao()->delete($id);
    }

    protected function _prepareSearchConditions($condition){

        //处理搜索字符串
        if(isset($condition['title'])){
            $condition['likeTitle']='%'.$condition['title'].'%';//title like
        }

        return $condition;
    }

    protected function getEntryDao()
    {
        return $this->biz->dao('Entry:EntryDao');
    }
}
